package com.fyh.service.impl;

import com.fyh.constant.MessageConstant;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.entity.job.JobPosition;
import com.fyh.entity.job.JobPositionRequest;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.JobFavorite;
import com.fyh.entity.user.SimpleCandidate;
import com.fyh.mapper.JobMapper;
import com.fyh.service.JobService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JobServiceImpl implements JobService {

    @Autowired
    JobMapper jobMapper;

    private static final Integer PAGE_SIZE = 10;

    /**
     * 获取技能标签列表
     *
     * @return
     */
    @Override
    public Map<String, List<String>> getTagList() {
        Map<String, List<String>> map = new HashMap<>();

        List<Long> ids = jobMapper.selectIndustryIdList();
        for (Long id : ids) {

            String name = jobMapper.selectIndustryNameById(id);

            List<String> tags = jobMapper.selectTagNameListByIndustryId(id);
            map.put(name, tags);
        }
        return map;
    }

    /**
     * 通过EmployerId获取职位列表
     *
     * @param employerId
     * @return
     */
    @Override
    public List<SimpleJobPosition> getSimpleJobListByEmployerId(Long employerId) {
        // 获取职位列表
        List<SimpleJobPosition> simpleJobList = jobMapper.selectSimpleJobListByEmployerId(employerId);
        // 获取本职位的投递的简历是否存在未读
        for (SimpleJobPosition simpleJob : simpleJobList) {
            Boolean exist = jobMapper.getDeliveryExistNotViewed(simpleJob.getPositionId());
            simpleJob.setIsRead(!exist);
        }
        return simpleJobList;
    }

    /**
     * 通过职位Id获取技能标签列表
     *
     * @param positionId
     * @return
     */
    @Override
    public List<String> getTagNameListByPositionId(Long positionId) {
        List<String> tagList = jobMapper.selectTagNameListByPositionId(positionId);
        return tagList;
    }

    /**
     * 获取职位详情
     *
     * @param positionId
     * @return
     */
    @Override
    public JobPosition getJobPositionDetail(Long positionId) {
        JobPosition jobPosition = jobMapper.selectJobPositionById(positionId);
        jobPosition.setTagList(jobMapper.selectTagNameListByPositionId(positionId));
        return jobPosition;
    }

    /**
     * 更新职位
     *
     * @param jobPosition
     */
    @Transactional
    @Override
    public void updateJobPosition(JobPosition jobPosition) {
        //修改职位信息
        jobMapper.updateJobPosition(jobPosition);
        //修改技能标签
        jobMapper.deleteTagByPositionId(jobPosition.getPositionId());
        for(String tagName : jobPosition.getTagList()){
            Long positionId = jobPosition.getPositionId();
            jobMapper.insertTag(positionId, tagName);
        }
    }

    /**
     * 通过职位名称和职位类型获取职位列表
     *
     * @param jobPositionRequest
     * @return
     */
    @Override
    public List<SimpleJobPosition> getSimpleJobListByNameAndType(JobPositionRequest jobPositionRequest) {
        // 分页查询
        PageHelper.offsetPage(jobPositionRequest.getOffset(), PAGE_SIZE);
        // 获取职位列表
        List<SimpleJobPosition> simpleJobList = jobMapper.getSimpleJobListByNameAndTypeAndTag(jobPositionRequest);
        for (SimpleJobPosition simpleJob : simpleJobList) {
            // 获取技能标签列表
            List<String> tagList = jobMapper.selectTagNameListByPositionId(simpleJob.getPositionId());
            simpleJob.setTagList(tagList);
        }
        return simpleJobList;
    }

    /**
     * 添加职位
     *
     * @param jobPosition
     */
    @Override
    @Transactional
    public void addJobPosition(JobPosition jobPosition) {
        //插入职位，回调职位Id
        jobMapper.insertJobPosition(jobPosition);
        //插入职位标签
        for(String tagName : jobPosition.getTagList()){
            jobMapper.insertTag(jobPosition.getPositionId(), tagName);
        }
    }

    /**
     * 删除职位
     *
     * @param positionId
     */
    @Override
    @Transactional
    public void deleteJobPosition(Long positionId) {
        //删除职位
        jobMapper.deleteJobPosition(positionId);
        //删除职位标
        jobMapper.deleteTagByPositionId(positionId);
    }

    /**
     * 更新职位状态
     * @param positionId
     */
    @Override
    public void updateJobStatus(Integer status, Long positionId) {
        jobMapper.updateJobStatus(status, positionId);
    }

    /**
     * 获取职位是否投递
     * @param positionId
     * @param userId
     */
    @Override
    public Boolean getDeliveryExist(Long positionId, Long userId) {
        return jobMapper.getDeliveryExist(positionId, userId);
    }

    /**
     * 投递职位
     * @param positionId
     * @param userId
     */
    @Override
    public void deliveryVitae(Long positionId, Long userId) {
        DeliveryRecord deliveryRecord = new DeliveryRecord();
        deliveryRecord.setPositionId(positionId);
        deliveryRecord.setUserId(userId);
        deliveryRecord.setCreateTime(LocalDateTime.now());
        deliveryRecord.setUpdateTime(LocalDateTime.now());
        jobMapper.insertDeliveryRecord(deliveryRecord);
    }

    /**
     * 获取职位下所有投递者id，投递时间和投递状态
     * @param positionId
     * @return
     */
    @Override
    public List<DeliveryRecord> getSimpleDeliveryRecordListList(Long positionId, String status) {
        List<DeliveryRecord> deliveryRecordList = jobMapper.getSimpleDeliveryRecordListList(positionId, status);
        return deliveryRecordList;
    }

    /**
     * 更新职位下投递者投递状态
     * @param deliveryRecord
     */
    @Override
    @Transactional
    public void updateDeliveryStatus(DeliveryRecord deliveryRecord) {
        jobMapper.updateDeliveryStatus(deliveryRecord);
    }

    /**
     * 通过收藏的职位基础信息列表
     * @param jobFavoriteList
     * @return
     */
    @Override
    public List<SimpleJobPosition> getSimpleJobListByPositionId(List<JobFavorite> jobFavoriteList) {
        List<SimpleJobPosition> simpleJobPositionList = new ArrayList<>();
        for(JobFavorite jobFavorite : jobFavoriteList){
            SimpleJobPosition simpleJobPosition = jobMapper.selectSimpleJobPositionById(jobFavorite.getPositionId());
            simpleJobPosition.setTagList(jobMapper.selectTagNameListByPositionId(jobFavorite.getPositionId()));
            simpleJobPositionList.add(simpleJobPosition);
        }
        return simpleJobPositionList;
    }

    /**
     * 通过职位Id获取 employerId
     * @param positionId
     * @return
     */
    @Override
    public Long getEmployerIdByPositionId(Long positionId) {
        Long employerId = jobMapper.getEmployerIdByPositionId(positionId);
        return employerId;
    }
}
